package com.banshuibao.bsb_android.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.DataQueryListAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.CaseQueryModel;
import com.banshuibao.bsb_android.model.KnowledgeModel;
import com.banshuibao.bsb_android.model.RepositoryQueryModel;
import com.banshuibao.bsb_android.model.RequestMoreModel;
import com.banshuibao.bsb_android.view.EditText_Clear;

import java.util.ArrayList;
import java.util.List;

public class DataQueryListActivity extends BaseActivity {

    private EditText_Clear editText_clear;
    private String content, tag;
    private RecyclerView recyclerView;
    private DataQueryListAdapter adapter;
    private RelativeLayout no_search;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_query_list);
        content = getIntent().getStringExtra("content");
        tag = getIntent().getStringExtra("tag");
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //   知识库repository     案例库case
        switch (tag) {
            case "repository":
                repositoryNet();
                break;
            case "case":
                requestCaseNet();
                break;
        }
    }

    public void initView() {
        no_search = findViewById(R.id.no_search);
        editText_clear = findViewById(R.id.et_search);
        editText_clear.setHint(content);
        editText_clear.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                finish();
                return false;
            }
        });
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new DataQueryListAdapter(this);
        adapter.setKey(tag, content);
        recyclerView.setAdapter(adapter);
    }

    //知识库
    public void repositoryNet() {
        RequestMoreModel moreModel = new RequestMoreModel();
        moreModel.setContent(content);
        showProgressDialog();
        HttpUtil.doPost(Constants.Url.KNOWLEDGE_SEARCH, moreModel, new HttpResponse(DataQueryListActivity.this, RepositoryQueryModel.class) {
            @Override
            public void onResult(Object obj) {
                RepositoryQueryModel repositoryQueryModel = (RepositoryQueryModel) obj;
                if (repositoryQueryModel != null) {
                    no_search.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    List<KnowledgeModel.DataBean> knowledgeTeacher = repositoryQueryModel.getData().getKnowledgeTeacher();
                    List<KnowledgeModel.DataBean> knowledgeCollect = repositoryQueryModel.getData().getKnowledgeCollect();
                    if (knowledgeTeacher != null && knowledgeTeacher.size() > 0 || knowledgeCollect != null && knowledgeCollect.size() > 0) {
                        no_search.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.VISIBLE);
                        adapter.clearAdapter();
                        adapter.setNavigation(knowledgeTeacher);
                        adapter.setCollection(knowledgeCollect);
                    } else {
                        no_search.setVisibility(View.VISIBLE);
                        recyclerView.setVisibility(View.GONE);
                    }
                } else {
                    no_search.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }

            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }

    //案例库
    public void requestCaseNet() {
        RequestMoreModel moreModel = new RequestMoreModel();
        moreModel.setContent(content);
        showProgressDialog();
        HttpUtil.doPost(Constants.Url.CASE_SEARCH, moreModel, new HttpResponse(DataQueryListActivity.this, CaseQueryModel.class) {
            @Override
            public void onResult(Object obj) {
                CaseQueryModel caseQueryModel = (CaseQueryModel) obj;
                if (caseQueryModel != null) {
                    no_search.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    List<KnowledgeModel.DataBean> caseTax = caseQueryModel.getData().getCaseTax();
                    List<KnowledgeModel.DataBean> caseCourt = caseQueryModel.getData().getCaseCourt();
                    List<KnowledgeModel.DataBean> caseCheck = caseQueryModel.getData().getCaseCheck();
                    if (caseTax != null && caseTax.size() > 0 || caseCourt != null && caseCourt.size() > 0 || caseCheck != null && caseCheck.size() > 0) {
                        no_search.setVisibility(View.GONE);
                        recyclerView.setVisibility(View.VISIBLE);
                        adapter.clearAdapter();
                        adapter.setTax(caseTax);
                        adapter.setJudicial(caseCourt);
                        adapter.setAuditing(caseCheck);
                    } else {
                        no_search.setVisibility(View.VISIBLE);
                        recyclerView.setVisibility(View.GONE);
                    }
                } else {
                    no_search.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                }

            }

            @Override
            public void responseEnd() {
                super.responseEnd();
                cancelProgressDialog();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adapter != null) {
            adapter.clearAdapter();
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (adapter != null) {
            adapter.clearAdapter();
        }
    }
}
